﻿/////Tạo tab/////////////////////////////////////////////////
function addTab(tabPanel, id, url, title) {
    var tab = tabPanel.getComponent(id);
    if (!tab) {
        tab = tabPanel.add({
            id: id,
            title: title,
            closable: true,
            autoLoad: {
                showMask: true,
                url: url,
                mode: 'iframe',
                maskMsg: ' Đang nạp dữ liệu, vui lòng chờ...'
            }
        });
        tab.on('activate', function() {
            //var item = MenuPanel1.menu.items.get(id + "_item");
            //                    if (item) {
            ////                        MenuPanel1.setSelection(item);
            //                    }
        }, this);
    }
    tabPanel.setActiveTab(tab);
    return tab;
}
/////Tạo tab mặc định (force: neu ton tai thi dong, con ko thi tao moi )////////////////////////////////////////
function addDefaultTab(id, url, title) {
    var tabPanel = pnTabPanel;
    var tab = tabPanel.getComponent(id);
    if (tab) {
        tabPanel.remove(tab);
    }
    addTab(tabPanel, id, url, title);
}
///// Toggle tab (đóng Tab, mở tab khác nếu chưa có hoặc reload lại tab nếu đã tồn tại)////////////////////
function toggleTab(closeId, openId, url, title) {
    var tabPanel = pnTabPanel;
    var tab = tabPanel.getComponent(closeId);
    if (tab != null) { tabPanel.remove(tab); }
    addDefaultTab(openId, url, title);
}
//////////// Create new window ///////////////////////////////////////
function newWindow(url, config) {
    new Ext.Window(Ext.apply({
        renderTo: Ext.getBody(),
        resizable: false,
        modal: false,
        closable: true,
        layout: "fit",
        maximizable: false,
        constrain: true,
        closeAction: "destroy",
        frame: true,
        id: Math.floor(Math.random() * 101086).toString(), // "new_window",
        listeners: {
            beforeshow: {
                fn: function (el) {
                    var height = Ext.getBody().getViewSize().height;
                    var width = Ext.getBody().getViewSize().width;
                    if (el.getSize().height > height) {
                        el.setHeight(height - 20)
                    }
                    if (el.getSize().width > width) {
                        el.setWidth(width - 100);
                    }
                }

            }
        },
        buttons: [
            {
                text: "Đóng",
                iconCls: "icon-accept",
                handler: function () {
                    this.findParentByType("window").destroy();
                }
            }
        ],
        autoLoad: {
            maskMsg: "Đang tải dữ liệu, vui lòng chờ...",
            showMask: true,
            mode: "iframe",
            url: url
        }
    }, config)).show();
}

var date = function(value, p, record) {
    if (value == null) return "";
    value = value.replace(" ", "T");
    var temp = value.split("T");
    var date = temp[0].split("-");
    var dateStr = date[2] + "/" + date[1] + "/" + date[0];
    return dateStr;
}

function reLoadActiveTab() {
    var tabPanel = pnTabPanel;
    var tab = tabPanel.getActiveTab();
    Ext.get(tab.id + "_IFrame").dom.contentWindow.reLoadData();
    Ext.WindowMgr.getActive().destroy();
}

function reLoadActiveTabWithType(type) {
    var tabPanel = pnTabPanel;
    var tab = tabPanel.getActiveTab();
    Ext.get(tab.id + "_IFrame").dom.contentWindow.reLoadDataWithType(type);
    Ext.WindowMgr.getActive().destroy();
}

function closeActiveTab() {
    var tabPanel = pnTabPanel;
    var tab = tabPanel.getActiveTab();
   // Ext.get(tab.id + "_IFrame").dom.contentWindow.reLoadData();
    Ext.WindowMgr.getActive().destroy();
}
